﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConferenceArranger.ConferenceComponent;

namespace ConferenceArranger.Strategy
{
        /// <summary>
        /// create Talk objects from reading input
        /// pass them t Sesion Processor
        /// </summary>
        class TalkProcessor
        {   
            //output
            List<Talk> talkList;

            public TalkProcessor()
            {
                
            }
            
            /// <summary>
            /// read the input file and create a list of Talk with title and interval
            /// </summary>
            /// <param name="fileName"></param>
            public void InitTalkList(string fileName)
            {
                try
                {
                    string[] lines = System.IO.File.ReadAllLines(fileName);
                    talkList = new List<Talk>();
                    //this dummy talk is used to calculation in SessionScheduler
                    // it will be ignored in all other cases
                    talkList.Add(new Talk("dummy", 0));
                    for (int i = 0; i < lines.Length; i++)
                    {
                        string line = lines[i];
                        int lastSpace = line.LastIndexOf(" ");
                        string title = line.Substring(0, lastSpace).Trim();
                        int interval;
                        try
                        {
                            interval = Int32.Parse(line.Substring(lastSpace + 1).Replace("min", ""));
                        }
                        catch (Exception ex)
                        {   //if no number is provided, give 5mins for the talk
                            interval = 5;
                        }
                        if(interval > 0 && title.Length >0 )
                            talkList.Add(new Talk(title, interval));
                    }
                }
                catch (FileNotFoundException ex)
                {
                    throw new FileNotFoundException("invalid file name", ex);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

            public List<Talk> GetOutput()
            {
                return talkList;
            }
        }
    
}
